package com.cn.lp.export.client.controller;

import com.cn.lp.export.client.ExportClient;
import com.cn.lp.export.client.base.context.CommandResult;
import com.cn.lp.export.client.base.context.ExportException;
import com.cn.lp.export.client.base.context.ResponseResult;
import com.cn.lp.export.client.entity.ExportService;
import com.cn.lp.export.client.export.ExportStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * Description: 请描述你的文件
 *
 * @author wuqirong
 * @date 2020-10-20
 * <p>
 * All rights Reserved, Designed www.xiao100.com
 */
@RestController
@RequestMapping("/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    @Autowired
    private ExportClient exportClient;

    /**
     * 添加导出请求
     *
     * 测试body {"businessType":"TEST","condition":{"queryCondition":{"bid":2}}}
     *
     * @return
     * @throws IOException
     */
    @PostMapping("/add")
    public CommandResult<ExportResponseDTO> add(@RequestBody ExportRequestDTO request) {
        if (request.getBusinessType() == null) {
            throw new ExportException("导出类型不能为空");
        }
        return new ResponseResult<ExportResponseDTO>()
            .setSuccess(true)
            .setResultCode(200)
            .setMessage(exportService.export(request));
    }

    /**
     * 查询导出结果
     *
     * @param taskId
     * @return
     */
    @GetMapping("/get/{taskId}")
    public CommandResult<Integer> get(@PathVariable String taskId) {
        ExportStatus status = exportClient.getTaskStatus(Long.parseLong(taskId));
        return new ResponseResult<Integer>()
            .setSuccess(true)
            .setResultCode(200)
            .setMessage(status.getId());
    }

}
